<?php

/*
 *
 */

class Model_User_Provisioning extends Model_BaseModel
{
    use Lib_BaseTraits;
    /**
     * 缓存前缀
     *
     * @var unknown
     */
    private static $tag = 'Model_User_Provisioning';
    private $table = "sys_provisioning";
    function __construct()
    {
        // 选择连接的数据库
        parent::_init('mediation');
    }


    /**获取全部数据
     * @return mixed
     */
    public function getListAll()
    {
        $sql = "SELECT * FROM {$this->table}  order by seq DESC ,id ";
        $data = [];
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchAll();
    }


    /**
     * @param $id
     * @return mixed
     */
    public function deleteById($id)
    {
        $sql = "DELETE FROM {$this->table} WHERE id=:id";
        $data = [
            ':id' => $id,
        ];
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        if ($res) {
            $this->dao->clearTag(self::$tag);
        }
        return $res;
    }

    /*
     * 存储数据
     */
    public function insertData($saveData)
    {
        foreach ($saveData as $key => $value) {
            $arrFields[] = '`' . $key . '`';
            $temp = ':' . $key;
            $dataA[$temp] = $value;
            $arrValues[] = $temp;
        }
        $sqlA = 'INSERT INTO `' . $this->table . '` (' . implode(',', $arrFields) . ') VALUES (' . implode(',', $arrValues) . ')';
        $uid = $this->dao->conn(false)->noCache()->preparedSql($sqlA, $dataA)->lastInsertId();
        $this->dao->clearTag(self::$tag);
        return $uid;
    }


    /**
     * 获取列表
     */
    public function getListByRoleId($provisioning_ids)
    {
        if (empty($provisioning_ids)) {
            return array();
        }
        $sql = "SELECT * FROM {$this->table} WHERE id in (".  $provisioning_ids. ")" ;
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, array())->fetchAll();
    }

}
